package com.medic.service.impl;



import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.medic.entity.User;
import com.medic.entity.Weichat;
import com.github.pagehelper.Page;
import com.medic.mapper.interf.UserMapper;
import com.medic.service.interf.UserServiceInterface;
import com.medic.util.CacheUtil;

@Service("userService") 
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class  UserServiceImpl implements UserServiceInterface {

	
	@Resource
	private UserMapper userMapper;
	

	
	@Resource
	private WeichatServiceImpl weichatService;
	
	
	@Override
	public boolean delete(User object) throws Exception {
		
		userMapper.delete(object);
		
		return true;
	}

	

	@Override
	public boolean save(User usr) throws Exception {
				
		userMapper.save(usr);
		
		Weichat wechat = new Weichat();
		wechat.setOpenid(usr.getOpenid());
		wechat.setRole("user");
		wechat.setRolename(usr.getName());
		wechat.setRoleid(usr.getId());
		
		weichatService.save(wechat);
		
		return true;
	}
	
	
	
	@Override
	public boolean update(User object) throws Exception {
		userMapper.update(object);
		return true;
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public User getUser(int userid) throws Exception {
		return userMapper.selectUserByID(userid);
	}



	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Page<User> getUser(int pageno,String where) throws Exception {
		PageHelper.startPage(pageno ,CacheUtil.getRole_pagesize());
		
		
		Page<User> page = (Page<User>)userMapper.selectUser(where);
		
		return page;
	}








	
	
	


}
